// pages/goods/goods.js
const util =require('../../utils/util')
const api =require('../../config/api')
Page({
  data: {
    info:[],//商品数据
    attribute:[],//商品参数
    issue:[],//常见问题数据
    comment:[],//评论
    brand:[],//制造商信息
    specificationList:[],//规格列表
    productList:[],//一个产品对应的不同产品
    content:"",//详情页的富文本
    // 类型和颜色数据
    typeWord:'请选择规格和颜色',
    colorWord:'',
    // 不同颜色的图片地址
    colorPicUrl:'',
    // 类型页面的显示与隐藏
    showType:false,
    // 不同类型选择 得出不同价格
    priceAll:'',
    productId:'',
    colorFlag:false,
    // 步进器价格
    number:1,
    goodsId:'',
    cartGoodsCount:'',
  },
  // 点击类型按钮 调出选择类型页面
  numberType(){
    this.setData({
      showType:true
    })
    
  },
  // 前往购物车
  goToCart(){
    console.log(1);
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  // 点击加入购物车  判断是否登录  是的话进入购物车 否的话进入登录页
  goCart(){
    let that =this
    if(wx.getStorageSync('token')){
      
      if(that.data.typeWord !='请选择规格和颜色' && that.data.typeWord||that.data.colorWord){
        util.request(api.cartAdd,{
          goodsId:that.data.goodsId,
          number:that.data.number,
          productId:that.data.productId,
        },'post').then(res=>{
          if(res.errno==711){
            wx.showToast({
              title: '库存不足，请重新选择商品数量',
              duration: 2000,
              icon:'none'
              });
          }else{
            that.setData({
              cartGoodsCount:res.data
            })
          }
          console.log(res);
        })
      }else{
        that.setData({
          showType:true
        })
        setTimeout(() => {
          wx.showToast({
          title: '请选择商品的种类型号',
          duration: 2000,
          icon:'none'
          });
        }, 300);
      }
    }else{
      wx.showToast({
        title: '您还未登录，请先登录',
        duration: 2000,
        icon:'none'
        });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/auth/login/login',
        })
      }, 1000);
      
    }
  },
  // 点击叉号或阴影区域 退出类型页面
  closeType(){
    this.setData({
      showType:false
    })
    
  },
  // 步进器函数
  onChange(event) {
    this.setData({
      number:event.detail
    })
    this.priceNumber()
    
  },
  // 点击不同规格 不同颜色 实现切换 共6个
  typeChoiceOne(e){
    console.log(e);
    let that =this
    let _specificationList =this.data.specificationList
    let productList =this.data.specificationList
    if(e.target.dataset.todo.name=='规格'){
      for(let i=0; i<1;i++){
        if(e.target.dataset.index==0){
          this.setData({
            ['specificationList['+i+'].valueList['+i+'].deleted']:true,
            ['specificationList['+i+'].valueList['+(i+1)+'].deleted']:false,
            typeWord:e.target.dataset.value
          })
        }else if(e.target.dataset.index==1){
          this.setData({
            ['specificationList['+i+'].valueList['+(i)+'].deleted']:false,
            ['specificationList['+i+'].valueList['+(i+1)+'].deleted']:true,
            typeWord:e.target.dataset.value,
          })
        }
      }
    }else if(e.target.dataset.todo.name=='颜色'){
      for(let j=1; j<2;j++){
      if(e.target.dataset.index==0){
        this.setData({
          ['specificationList['+j+'].valueList['+(j)+'].deleted']:false,
          ['specificationList['+j+'].valueList['+(j+1)+'].deleted']:false,
          ['specificationList['+j+'].valueList['+(j-1)+'].deleted']:true,
          colorPicUrl:e.target.dataset.picurl,
          colorWord:e.target.dataset.value,
        })
      }else if(e.target.dataset.index==1){
        this.setData({
          ['specificationList['+j+'].valueList['+(j)+'].deleted']:true,
          ['specificationList['+j+'].valueList['+(j+1)+'].deleted']:false,
          ['specificationList['+j+'].valueList['+(j-1)+'].deleted']:false,
          colorPicUrl:e.target.dataset.picurl,
          colorWord:e.target.dataset.value,
        })
      }else if(e.target.dataset.index==2){
        this.setData({
          ['specificationList['+j+'].valueList['+(j)+'].deleted']:false,
          ['specificationList['+j+'].valueList['+(j-1)+'].deleted']:false,
          ['specificationList['+j+'].valueList['+(j+1)+'].deleted']:true,
          colorPicUrl:e.target.dataset.picurl,
          colorWord:e.target.dataset.value,
        })
      }
    }
        
    }
    this.priceNumber()
  },
  priceNumber(){
    let productList =this.data.productList
    let that =this
    productList.forEach(item=>{
        for(let i=0 ; i<item.specifications.length;i++){
          if(item.specifications[i]==that.data.typeWord&&item.specifications[i+1]==that.data.colorWord){
            that.setData({
              priceAll:item.price,
              productId:item.id,
            })
            return
          }
        }
    })
    console.log(this.data.priceAll);
    console.log(this.data.productId);
  },

  onLoad(options) {
    console.log(options)
    this.goodsDetail(options.id)
    this.setData({
      goodsId:options.id
    })
  },

  // 封装调用商品详情接口
  goodsDetail:function(id) {
    let that =this
    util.request(api.goodsDetail,{id},'get').then(res=>{
      console.log(res);
      if(res.errno==0){
        that.setData({
          info:res.data.info,
          issue:res.data.issue,
          attribute:res.data.attribute,
          brand:res.data.brand,
          comment:res.data.comment,
          productList:res.data.productList,
          specificationList:res.data.specificationList,
          content:res.data.info.detail.replace(/\<img/gi,'<img class="diy-img" '),
          colorPicUrl:res.data.info.picUrl,
          priceAll:res.data.info.retailPrice,
        })
        console.log(that.data.info);
        console.log(that.data.specificationList);
        console.log(that.data.productList);
      }
    })
    util.request(api.cartIndex).then(res=>{
      console.log(res);
      if(res.errno==0){
        that.setData({
          cartGoodsCount:res.data.cartTotal.goodsCount
        })
      }
    })
  },




})